package com.hengda.museumonline.utils;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.BaseLoadMoreHeaderAdapter;
import com.hengda.museumonline.adapter.ErrorAdapter;
import com.hengda.museumonline.adapter.MainBannerAdapter;
import com.hengda.museumonline.entity.DomesticBean;
import com.hengda.museumonline.entity.TopnewsBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.http.GetData;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.view.AutoPlayViewPager;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/10/17 16:24
 * @E-mail：jeffy12138@126.com
 */
public class AutoPlayUtil {
    private MainActivity mainActivity;
    private TopnewsBean topnewsBean;
    private AutoPlayViewPager autoPlayViewPager;
    private int pid;

    public AutoPlayUtil(MainActivity mainActivity, AutoPlayViewPager autoPlayViewPager, int pid) {
        this.mainActivity = mainActivity;
        this.autoPlayViewPager = autoPlayViewPager;
        this.pid = pid;
    }

    public void getDataFromServer() {
        Request request = GetData.getNews(mainActivity, MyApplication.url + "news/topnews", "a",
                PackageUtil.getAppPackageInfo(mainActivity).versionName, pid, 0, 20);
        CallServer.getRequestInstance().add(mainActivity, 0, request, listener);
    }

    OnResponseListener listener = new OnResponseListener() {
        @Override
        public void onStart(int what) {

        }

        @Override
        public void onSucceed(int what, Response response) {
            int responseCode = response.getHeaders().getResponseCode();// 服务器响应码
            Gson gson = new Gson();
            System.out.println("结果：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + response.get().toString());
            topnewsBean = gson.fromJson(response.get().toString(), TopnewsBean.class);
            if (topnewsBean != null && topnewsBean.getData() != null && topnewsBean.getData().size() > 0) {
                autoPlayViewPager.setAdapter(new MainBannerAdapter(topnewsBean));
                autoPlayViewPager.setCurrentItem(topnewsBean.getData().size() * 100);
                autoPlayViewPager.start();
                autoPlayViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            } else {

            }

        }

        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
            System.out.println("失败：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + exception);
        }

        @Override
        public void onFinish(int what) {

        }
    };
}
